﻿Imports System.Data.OleDb
Public Class frmNotes
    Private Sub frmNotes_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'PsychologicalStateTrackerDataSet.tblNotes' table. You can move, or remove it, as needed.
        Me.TblNotesTableAdapter.Fill(Me.PsychologicalStateTrackerDataSet.tblNotes)

    End Sub

    Private Sub btnCancel_Click(sender As System.Object, e As System.EventArgs) Handles btnCancel.Click
        'Empty form
        DatePicker.Text = ""
        TimePicker.Text = ""
        txtThoughts.Text = ""
        txtEmotions.Text = ""
        txtPhysicalConsequences.Text = ""
        cmbScore.Text = ""

        'Load Display Notes page
        Me.TabPageDisplayNotes.Show()
        TabControl.SelectedIndex = 0

    End Sub

    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
        If txtThoughts.Text = "" Then

            MsgBox("You have not entered your Thoughts.")

        ElseIf txtEmotions.Text = "" Then

            MsgBox("You have not entered your Emotions.")

            txtEmotions.Focus()

        ElseIf txtPhysicalConsequences.Text = "" Then

            MsgBox("You have not entered your Physical Consequences.")

            txtPhysicalConsequences.Focus()

        ElseIf cmbScore.Text = "" Then

            MsgBox("You have not entered your Score.")

            cmbScore.Focus()

        Else

            Dim connection As OleDb.OleDbConnection

            Dim mydb, mystr As String

            mystr = ("Provider=Microsoft.JET.OLEDB.4.0;" & _
                     "Data Source=|DataDirectory|\PsychologicalStateTracker.mdb")

            connection = New OleDb.OleDbConnection(mystr)

            connection.Open()

            mydb = "INSERT INTO tblNotes (fldDate,fldTime,fldThoughts,fldEmotions,fldPhysicalConsequences,fldScore) values ('" & DatePicker.Text & "','" & TimePicker.Text & "','" & txtThoughts.Text & "','" & txtEmotions.Text & "','" & txtPhysicalConsequences.Text & "','" & cmbScore.Text & "')"

            Dim run = New OleDb.OleDbCommand

            Try

                run = New OleDbCommand(mydb, connection)

                run.ExecuteNonQuery()

                'Load DisplayNotes tab
                Me.TabPageDisplayNotes.Show()
                TabControl.SelectedIndex = 0

                'Display message box
                MsgBox("Note Added to Database.")

                'Close connection
                connection.Close()

                'Reset Form Fields
                DatePicker.Text = ""
                TimePicker.Text = ""
                txtThoughts.Text = ""
                txtEmotions.Text = ""
                txtPhysicalConsequences.Text = ""
                cmbScore.Text = ""
                DatePicker.Focus()

            Catch ex As Exception

                MsgBox(ex.Message, MsgBoxStyle.Critical, "Oledb Error")

            End Try

        End If
    End Sub

    Private Sub ToolStripAdd_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripAdd.Click
        Me.TabPageAddNote.Show()
        TabControl.SelectedIndex = 1
    End Sub

    Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
        If txtSearchThoughts.Text IsNot "" Then
            ListViewSearch.Focus()

            For i = 0 To ListViewSearch.Items.Count - 1
                'Searches second column: SubItems(2)
                If ListViewSearch.Items(i).SubItems(2).Text = txtSearchThoughts.Text Then
                    ListViewSearch.Items(i).Selected = True
                End If
            Next

        ElseIf txtSearchEmotions.Text IsNot "" Then
            ListViewSearch.Focus()

            For i = 0 To ListViewSearch.Items.Count - 1
                'Searches third column: SubItems(3)
                If ListViewSearch.Items(i).SubItems(3).Text = txtSearchEmotions.Text Then
                    ListViewSearch.Items(i).Selected = True
                End If
            Next

        ElseIf txtSearchPhysicalConsequences.Text IsNot "" Then
            ListViewSearch.Focus()

            For i = 0 To ListViewSearch.Items.Count - 1
                'Searches fourth column: SubItems(4)
                If ListViewSearch.Items(i).SubItems(4).Text = txtSearchPhysicalConsequences.Text Then
                    ListViewSearch.Items(i).Selected = True
                End If
            Next

        ElseIf txtSearchScore.Text IsNot "" Then
            ListViewSearch.Focus()

            For i = 0 To ListViewSearch.Items.Count - 1
                'Searches fifth column: SubItems(5)
                If ListViewSearch.Items(i).SubItems(5).Text = txtSearchScore.Text Then
                    ListViewSearch.Items(i).Selected = True
                End If
            Next

        ElseIf txtSearchThoughts.Text = "" Or txtSearchEmotions.Text = "" Or txtSearchPhysicalConsequences.Text Or txtSearchScore.Text Then
            MsgBox("Please enter some text to initiate a search.")
        End If

    End Sub

    Private Sub ToolStripDelete_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripDelete.Click
        'Removes selected row
        For i As Integer = ListViewSearch.Items.Count - 1 To 0 Step -1
            If ListViewSearch.Items(i).Selected Then ListViewSearch.Items.RemoveAt(i)
        Next

    End Sub

    Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click
        'Empty Search form
        txtSearchThoughts.Text = ""
        txtSearchEmotions.Text = ""
        txtSearchPhysicalConsequences.Text = ""
        txtSearchScore.Text = ""
    End Sub

    Private Sub TimePicker_ValueChanged(sender As System.Object, e As System.EventArgs) Handles TimePicker.ValueChanged

    End Sub

    Private Sub ListViewSearch_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListViewSearch.SelectedIndexChanged

    End Sub
End Class